"""
@Project:p8项目
@File:courses.py
@Author:柴奇峰
@Date:9:40
"""

from model import db
from datetime import datetime
from model.user_model import *

# 课程标签
class CourseTag(db.Model):
    __tablename__ = 'tb_course_tag'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True,doc='标签的id')
    tag_name= db.Column(db.String(32),doc='标签名称')
    sequence = db.Column(db.Integer,doc='展示顺序')
    course= db.relationship('Course',secondary='tb_course_and_tag',backref=db.backref('course_tag'))


# 标签 与 课程
class CourseAndTag(db.Model):
    __tablename__ ='tb_course_and_tag'
    id = db.Column(db.Integer,primary_key=True)
    course_id = db.Column(db.Integer,db.ForeignKey('tb_course.id',ondelete='CASCADE'),doc='课程主键')
    tag_id = db.Column(db.Integer, db.ForeignKey('tb_course_tag.id',ondelete='CASCADE'), doc='标签主键')

# 类别表
class Category(db.Model):
    __tablename__ = 'tb_category'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True,doc='类别id')
    name = db.Column(db.String(32),doc='类别名')
    course = db.relationship('Course',backref = 'Course_Category')


# 用户关注课程
class UserCourse(db.Model):
    __tablename__ = 'user_course_tb'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    uid = db.Column(db.Integer, db.ForeignKey('user_tb.uid'), doc="用户ID")
    cid = db.Column(db.Integer, db.ForeignKey('tb_course.id'),doc="课程ID")
    is_delete = db.Column(db.Boolean, default=0, doc="状态(1, 可用;0, 不可用)")
    create_time = db.Column(db.DateTime, default=datetime.now, doc="创建时间")
    update_time = db.Column(db.DateTime, default=datetime.now, doc="更新时间")
    sequence = db.Column(db.Integer, default=0, doc='序号')


# 课程表
class Course(db.Model):
    __tablename__ = 'tb_course'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True,doc='课程id')
    course_name = db.Column(db.String(32),doc='课程名称')
    course_status = db.Column(db.Integer,default=1,doc='0即将上线,1已上线')
    course_img = db.Column(db.String(600),doc='课程封面')
    attention = db.Column(db.Integer,default=0,doc='关注人数')
    course_introduce = db.Column(db.String(256),doc='课程价绍')
    learner = db.Column(db.Integer,default=0,doc='课程人数')
    course_type = db.Column(db.Integer,db.ForeignKey('tb_category.id'),doc='课程类别')
    course_time = db.Column(db.DateTime,default=datetime.now(),doc='创建时间')
    chapter = db.relationship('Chapters',backref='Chapter_Course')
    comment = db.relationship('Comments',backref='Comment_Course')
    users = db.relationship('User', secondary='user_course_tb', backref=db.backref('course'))



# 课程的章节
class Chapters(db.Model):
    __tablename__ = 'tb_chapter'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True,doc='章节id')
    title = db.Column(db.String(32),doc='章节名')
    serial_num = db.Column(db.Integer,doc='展示顺序')
    course = db.Column(db.Integer,db.ForeignKey('tb_course.id',ondelete='CASCADE'),doc='课程')
    sections = db.relationship('Sections',backref = 'section_chapter')

# 章节的小节
class Sections(db.Model):
    __tablename__ = 'tb_section'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True,doc='小节id')
    section_title = db.Column(db.String(32),doc='小节名')
    serial_num = db.Column(db.Integer,doc='展示顺序')
    learn_time = db.Column(db.Float,default=0.00,doc='视频时长')
    video_url = db.Column(db.String(256),doc='视频地址')
    like_count = db.Column(db.Integer,default=0,doc='点赞次数')
    course = db.Column(db.Integer,db.ForeignKey('tb_chapter.id',ondelete='CASCADE'),doc='章节')

# 课程评论
class Comments(db.Model):
    __tablename__ = 'tb_comment'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True,doc='评论id')
    user = db.Column(db.Integer,db.ForeignKey('user_tb.uid',ondelete='CASCADE'),doc='评论人')
    comment_time = db.Column(db.DateTime,default=datetime.now(),doc='评论时间')
    comment_content = db.Column(db.Text,doc='评论内容')
    zan_num = db.Column(db.Integer,default=0,doc='点赞次数')
    parent_id = db.Column(db.Integer,db.ForeignKey('tb_comment.id'),nullable=True,doc='被评论的id')
    course = db.Column(db.Integer,db.ForeignKey('tb_course.id'),doc='课程的id')
    reply_count = db.Column(db.Integer,default=0,doc='回复数')
    is_tap = db.Column(db.Boolean,default=0,doc='是否置顶')
    is_favorite = db.Column(db.Boolean, default=0, doc='是否点赞')
